/*
 * @Description: store
 * @Author: 
 * @Date: 2021-07-14 15:43:02
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-07-28 19:30:05
 */
import Vue from 'vue'
import Vuex from 'vuex'
import { getDic } from '@/api';

Vue.use(Vuex)
const type = {
  PERSON_DETAIL: 'PERSON_DETAIL',
  PERSON_ID: 'PERSON_ID',
  FINISH_CHECK: 'FINISH_CHECK',
  SET_IS_SELECT_ROOM: 'SET_IS_SELECT_ROOM',
  SET_DICTIONARIES: 'SET_DICTIONARIES',
  SET_AUTH_STATUS: 'SET_AUTH_STATUS',
  SET_WXCODE: 'SET_WXCODE',
  REMOVE_DATA_IGP: 'REMOVE_DATA_IGP',
}

const store = new Vuex.Store({
  state: { 
    isAuth: false, // 是否登陆且通过认证
    personDetail: [],  // 存储选房流程房间内的人员信息
    personId: [],  // 存储选房流程已选人员的id
    isSelectRoom: false, // 标志是否选房流程。 影响成功页面的按钮及事件判断
    dictionaries:{},     // 储存数据字典
  },
  mutations: {
    [type.PERSON_DETAIL]: (state, personDetail) => {
      state.personDetail = personDetail
    },
    [type.PERSON_ID]: (state, personId) => {
      state.personId = personId
    },
    [type.FINISH_CHECK]: (state) => {
      state.personDetail = state.personDetail.filter(item => state.personId.indexOf(item.personId) == -1)
      state.personId = []
    },
    [type.SET_IS_SELECT_ROOM]: (state, flag) => {
      state.isSelectRoom = flag
    },
    [type.SET_DICTIONARIES]: (state,params) => {
      state.dictionaries[params.type] = params.data
    },
    [type.SET_AUTH_STATUS]: (state, status) => {
      if (status) {
        uni.showTabBar()
      } else {
        uni.hideTabBar()
      }
      state.isAuth = status
    },
    [type.SET_WXCODE]: (state, code) => {
      state.wxCode = code
    },
    [type.REMOVE_DATA_IGP]:(state, status)=>{
      try {
        uni.removeStorage({
          key: 'dataGad7'
        })
        uni.removeStorage({
          key: 'dataPhq9'
        })
        uni.removeStorage({
          key: 'dataIsi'
        })
        uni.removeStorage({
          key: 'healthInspectRes'
        })
      } catch (error) { }
    }
  },
  actions:{
    // 传入type获取对应字典
    getDic({state,commit},type){
      let { dictionaries } = state;
      if(!dictionaries[type]){
        let dic = getDic(type)
        commit('SET_DICTIONARIES',{
          type,
          data:dic
        })
      }
      return dictionaries[type]
    }
  }
})

export default store